package com.internetCafes.spms.web.customer.common.component.customer.impl;

import com.internetCafes.spms.common.utils.ShiroUtils;
import com.internetCafes.spms.core.exception.RRException;
import com.internetCafes.spms.core.exception.RRExceptionCodeEnum;
import com.internetCafes.spms.web.customer.bizservice.customer.IEntCustomerBizService;
import com.internetCafes.spms.web.customer.common.component.customer.IEntCustomerComponent;
import com.internetCafes.spms.web.customer.common.component.msg.impl.MsgComponent;
import com.internetCafes.spms.web.customer.common.cont.CommonConst;
import com.internetCafes.spms.web.customer.common.cont.CustomerConst;
import com.internetCafes.spms.web.customer.entity.customer.Customer;
import com.internetCafes.spms.web.customer.model.ExcelImportResultRes;
import com.internetCafes.spms.web.customer.model.customer.CustomerBatchSaveReq;
import com.internetCafes.spms.web.customer.model.customer.CustomerSaveReq;
import com.internetCafes.spms.web.customer.model.customer.CustomerUpdateReq;
import com.internetCafes.spms.web.customer.service.customer.ICustomerFlowService;
import com.internetCafes.spms.web.customer.service.customer.ICustomerQualiImageService;
import com.internetCafes.spms.web.customer.service.customer.ICustomerQualiService;
import com.internetCafes.spms.web.customer.service.customer.ICustomerService;
import com.sms.common.util.hutool.core.bean.BeanUtil;
import com.sms.common.util.hutool.core.collection.CollectionUtil;
import com.sms.common.util.hutool.core.date.DateUtil;
import com.sms.common.util.hutool.core.util.ObjectUtil;
import com.sms.common.util.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 区域客户管理
 */
@Component("areaCustomerComponent")
public class AreaCustomerComponent implements IEntCustomerComponent {

    @Autowired
    private ICustomerService customerService;
    @Autowired
    private ICustomerQualiService qualiService;
    @Autowired
    private ICustomerQualiImageService qualiImageService;
    @Autowired
    private ICustomerFlowService flowService;
    @Autowired
    private MsgComponent msgComponent;
    @Autowired
    private IEntCustomerBizService customerBizService;

    /**
     * 列表名称
     */
    private final String MENU_NAME = "区域名单";

    @Override
    public ExcelImportResultRes batchSave(CustomerBatchSaveReq req) {
        return customerBizService.entBatchSave(req, MENU_NAME);
    }

    @Override
    public boolean update(CustomerUpdateReq req) {
        // 必填字段判断
        if (StrUtil.isBlank(req.getName()) || ObjectUtil.isNull(req.getCityId())
                || ObjectUtil.isNull(req.getAreaId()) || StrUtil.isBlank(req.getCertificateCode())
                || CollectionUtil.isEmpty(req.getQualiIdList()) || StrUtil.isBlank(req.getContactsName())) {
            throw new RRException(RRExceptionCodeEnum.PARAM_ERROR);
        }
        // 保存客户信息
        Customer customerDo = new Customer();
        BeanUtil.copyProperties(req, customerDo);
        if (StrUtil.isNotBlank(req.getSecurityLicenseExpiryDate())) {
            customerDo.setSecurityLicenseExpiryDate(DateUtil.parseDate(req.getSecurityLicenseExpiryDate()));
        }
        // 保存资质信息
        qualiService.config(customerDo.getId(), req.getQualiIdList());

        // 保存资质证明图片信息
        qualiImageService.config(customerDo.getId(), req.getQualiImageList());

        return customerService.updateById(customerDo);
    }

    /**
     * 保存区域客户信息
     *
     * @param req 客户管理数据
     * @return true/false
     */
    @Override
    public boolean save(CustomerSaveReq req) {
        // 必填字段判断
        if (StrUtil.isBlank(req.getName()) || ObjectUtil.isNull(req.getCityId())
                || ObjectUtil.isNull(req.getAreaId()) || StrUtil.isBlank(req.getCertificateCode())
                || CollectionUtil.isEmpty(req.getQualiIdList()) || StrUtil.isBlank(req.getContactsName())) {
            throw new RRException(RRExceptionCodeEnum.PARAM_ERROR);
        }

        // 保存客户信息
        Customer customerDo = new Customer();
        BeanUtil.copyProperties(req, customerDo);
        if (StrUtil.isNotBlank(req.getSecurityLicenseExpiryDate())) {
            customerDo.setSecurityLicenseExpiryDate(DateUtil.parseDate(req.getSecurityLicenseExpiryDate()));
        }
        customerDo.setMainlyFlag(CommonConst.FlagEnum.NOT.getId())
                .setThisYearDealFlag(CommonConst.FlagEnum.NOT.getId())
                .setLastYearDealFlag(CommonConst.FlagEnum.NOT.getId())
                .setFollowUserId(ShiroUtils.getUserId());
        customerService.save(customerDo);

        // 保存资质信息
        qualiService.config(customerDo.getId(), req.getQualiIdList());

        // 保存资质证明图片信息
        qualiImageService.config(customerDo.getId(), req.getQualiImageList());

        // 流转记录 创建
        flowService.add(customerDo.getId(), CustomerConst.FlowActionTag.CREATE.getId(),
                null, null, ShiroUtils.getUserId(),
                ShiroUtils.getUserEntity().getNickName());

        // 创建消息
        msgComponent.customerSave(customerDo, MENU_NAME);
        return true;
    }
}
